<?php
/*
 * index.action.php     Zhuayi 帐号模块
 *
 * @copyright    (C) 2005 - 2010  zhuayi
 * @licenes      http://www.zhuayi.net
 * @lastmodify   2010-10-28
 * @author       zhuayi
 * @QQ			 2179942
 */
class account_action extends zhuayi
{

	function __construct()
	{
		parent::__construct();
		
		/* 验证登录 */
		$this->user = member_action::verify();
		
		$this->db = parent::db();
	}
	
	
	function index()
	{
		/* 获取API接口列表 */
		$api = api_modle::api_list();
		require $this->load_tpl('account_index');
	}
	
	/* 管理帐号 */
	function manage($api)
	{
		if (empty($api))
		{
			message::showmsg('错误的来路...','/account');
		}
		
		$api = $info = api_modle::api(array('api'=>$api));
		
		$list = account_modle::account_list(array('api'=>$api['id']));
		//$this->test($api);
		require $this->load_tpl('account_manage');
	}
	
	/* 帐号列表 */
	function lists()
	{
		$list = account_modle::account_list(array('mid'=>$this->user['id']));
		echo json_encode($list);
		
	}
	
	/* 添加帐号 */
	function add($api)
	{
		if (empty($api))
		{
			exit('错误的来路');
		}

		require $this->load_tpl('account_add');
	}
	
	/* 账号入库 */
	function info()
	{
		$cookie = cookie::ret_cookie('user');
		
		/* 取API */
		$api = api_modle::api(array('api'=>$_POST['api']));
		
		/* 查询帐号总数 */
		$maxnum = account_modle::account_num(array('mid'=>$cookie['id'],'api'=>$api['id']));
		
		if ($maxnum >= $this->maxnum)
		{
			exit(message::ret_json('-1','没有绑定的余额...'));
		}
	
		
		$app = $this->load_class($api['api'].'_action');
		
		/* 查询帐号是否存在 */
		$reset = account_modle::account(array('username'=>$_POST['username'],'a.api'=>$api['id']),0);
		if (!empty($reset))
		{
			exit(message::ret_json('-1','已存在此帐号...'));
		}
		
		
		$reset['mid'] = $cookie['id'];
		$reset['username'] = $_POST['username'];
		$reset['password'] = $_POST['password'];
		$reset['api'] = $api['id'];
		
		
		/* 如果不是模拟登录 则跳转去授权 */
		if ($api['landing'] == 1)
		{
			
			$id = account_modle::account_info($reset);;

			exit(message::ret_json('-2',$app->login($id)));
			exit;
		}
		
		/* 登录帐号 */
		$return = $app->login($_POST);
		
		
		if ($return['code_en'] == '-1')
		{
			exit(message::ret_json('-1','录入失败...'));
		}
		$reset = array_merge($reset,$return);
		account_modle::account_info($reset);
		
		exit(message::ret_json('1',$reset));
		
	}
	
	function landing($array,$api)
	{

		$this->db = parent::db();
		account_modle::account_info($array);
		
		header("Location:/member/guide/3/".$api);
	}
	
	/* 获取帐号 */
	function account($id)
	{
		if (empty($id))
		{
			$id = $this->user['default'];
		}
		
		/* 获取帐号 */
		$account = account_modle::account(array('a.id'=>$id));
		//$account['token'] = strings::json_dearray($account['token']);
		return $account;
	}



}
?>